package com.atguigu.demo.controller;

import com.atguigu.demo.entity.Schedule;
import com.atguigu.demo.service.api.ScheduleService;
import com.atguigu.demo.utils.R;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;

    @GetMapping()
    public R getAllSchedule() {

        try {
            // 调用 Service 方法获取数据
            List<Schedule> scheduleList = scheduleService.getAll();

            // 如果操作成功：把查询结果数据封装到 R 对象中
            return R.ok(scheduleList);
        } catch (Exception e) {
            // 如果操作失败：把异常信息
            return R.fail(null);
        }
    }

    @DeleteMapping("/{id}")
    public R removeScheduleById(@PathVariable("id") Integer id) {
        try {
            scheduleService.removeById(id);

            return R.ok(null);
        } catch (Exception e) {
            return R.fail(null);
        }
    }

    @PostMapping()
    public R saveSchedule(@RequestBody Schedule schedule) {
        try {
            scheduleService.saveSchedule(schedule);
            return R.ok(null);
        } catch (Exception e) {
            return R.fail(null);
        }
    }

    @PutMapping
    public R updateSchedule(@RequestBody Schedule schedule) {
        try {
            scheduleService.updateSchedule(schedule);
            return R.ok(null);
        } catch (Exception e) {
            return R.fail(null);
        }
    }
}
